<?php
/*
  Langchange plugin
  (P) PSNet, 2008 - 2012
  http://psnet.lookformp3.net/
  http://livestreet.ru/profile/PSNet/
  http://livestreetcms.com/profile/PSNet/
*/

class PluginLangchange_HookLangchange extends Hook {
  public function RegisterHook () {
    $this -> AddHook ('lang_init_start', 'ChangeLanguage');
    $this -> AddHook ('init_action', 'AddStylesAndJS');
    $this -> AddHook ('template_body_begin', 'BodyBegin');
  }
  
  // ---

  public function AddStylesAndJS () {
    $sTemplateWebPath = Plugin::GetTemplateWebPath (__CLASS__);
    $this -> Viewer_AppendStyle ($sTemplateWebPath . 'css/style.css');
  }

  // ---

  public function BodyBegin () {
    $this -> Viewer_Assign ('AllLanguages', Config::Get ('plugin.langchange.Languages'));
    $this -> Viewer_Assign ('oCurrentLanguage', Config::Get ('lang.current'));
    $this -> Viewer_Assign ('URLToChangeLang', Config::Get ('plugin.langchange.LangChangeParameter'));
    return $this -> Viewer_Fetch (Plugin::GetTemplatePath (__CLASS__) . 'body_begin.tpl');
  }

  // ---

  public function ChangeLanguage () {
    $TLanguages = Config::Get ('plugin.langchange.Languages');
    if (!$TLanguages) return 0;

    $NewLangGET = getRequest (Config::Get ('plugin.langchange.LangChangeParameter'), null, 'get');

    @$NewLangSession = &$_SESSION ['langchange.lang'];

    $OkSetUpLanguage = false;
    if (in_array ($NewLangGET, $TLanguages)) {
      $NewLangSession = $NewLangGET;
      $OkSetUpLanguage = true;
    } elseif (in_array ($NewLangSession, $TLanguages)) {
      $OkSetUpLanguage = true;
    }
    if ($OkSetUpLanguage) {
      Config::Set ('lang.current', $NewLangSession);
    }
  }

}

?>